package com.zhaoyizhe.demo.controller;

import com.zhaoyizhe.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

//@RestController
@Controller
@ResponseBody
@RequestMapping("/request")
public class RequestController {
    @RequestMapping(value = "/request1",method = {RequestMethod.GET,RequestMethod.POST})
    public String request1(String userName){
        System.out.println("userName:" + userName );
        return "userName: " + userName;
    }

    @RequestMapping(value = "/request2",method = {RequestMethod.GET,RequestMethod.POST})
    public String request2(@RequestParam(value = "id",required = false) Integer userId){
        System.out.println("接收到参数Id: " + userId );
        return "接收到参数: " + userId;
    }

    @RequestMapping(value = "/request3",method = {RequestMethod.GET,RequestMethod.POST})
    public String request3(UserInfo userInfo){
        System.out.println(userInfo);
        return "接收到参数: "+userInfo;
    }

    @RequestMapping(value = "/request4",method = {RequestMethod.GET,RequestMethod.POST})
    public String request4(String[] strings){
        System.out.println(Arrays.toString(strings));
        return "接收到String数组: "+Arrays.toString(strings);
    }

    @RequestMapping(value = "/request5",method = {RequestMethod.GET,RequestMethod.POST})
    public String request5(@RequestParam("list") List<Integer> list){
        System.out.println(list);
        return "接收到集合list: "+ list;
    }

    @RequestMapping(value = "/request6",method = {RequestMethod.GET,RequestMethod.POST})
    public String request6(@RequestBody UserInfo userInfo){
        System.out.println(userInfo);
        return "接收到对象: " + userInfo;
    }

    @RequestMapping(value = "/request7/{age}/{name}",method = {RequestMethod.GET,RequestMethod.POST})
    public String request7(@PathVariable("age") Integer age,@PathVariable("name")String name){
        System.out.println("age: "+age + " name: "+name);
        return "接收到参数age: " + age +" name: "+ name;
    }

    @RequestMapping(value = "/request8",method = {RequestMethod.GET,RequestMethod.POST})
    public String request8(@RequestPart("file1") MultipartFile file){
        String fileName = file.getName();
        String fileType = file.getContentType();
        String fileName2 = file.getOriginalFilename();
        System.out.println(fileName);
        System.out.println(fileType);
        System.out.println(fileName2);
        return "接收到File: " + fileName2;
    }

    @RequestMapping("/getCookie1")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
//            System.out.println(cookie.getName() + " : " + cookie.getValue());
            if ("name".equals(cookie.getName())){
                System.out.println(cookie.getValue());
            }
            if ("age".equals(cookie.getName())){
                System.out.println(cookie.getValue());
            }
        }
        return "获取到Cookie";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name") Cookie cookie){
        System.out.println(cookie.getName() +":" +cookie.getValue());
        return "获取到Cookie :" + cookie.getValue();
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        if (session != null) {
            session.setAttribute("name", "zhangSan");
            session.setAttribute("age", 18);
        }
        return "session设置完毕";
    }

    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        if (session != null){
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("name1")); // null
        }
        return "获取Session成功";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        System.out.println(session.getAttribute("name"));
        System.out.println(session.getAttribute("age"));
        return "获取Session成功";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") Object session){
        System.out.println(session.toString());
        return "获取Session成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent: " + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "userAgent: " + userAgent;
    }


}
